package com.tuanzi.loan.business.service.statistics.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.repository.statistics.UnionLoginStatRepository;
import com.tuanzi.loan.business.service.statistics.UnionLoginStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.UnionLoginStatRequest;
import com.tuanzi.loan.vo.response.statistics.UnionLoginStatResponse;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class UnionLoginStatServiceImpl implements UnionLoginStatService {
	
	private static final String oldUser="0";
	private static final String newUser="1";

	@Autowired
	private UnionLoginStatRepository unionLoginStatRepository;

	@Override
	public Page<UnionLoginStatResponse> list(PageSearcher<UnionLoginStatRequest> pageSearcher) {
		Page<UnionLoginStatResponse> list = unionLoginStatRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
		return setUnionStatResponseOtherPorperties(list, pageSearcher.getSearchCondition());
	}

	@Override
	public Page<UnionLoginStatResponse> setUnionStatResponseOtherPorperties(Page<UnionLoginStatResponse> list,
			UnionLoginStatRequest searchRequest) {
		
		for (UnionLoginStatResponse response : list) {
			if(StringUtils.isBlank(searchRequest.getPlatform())){
				response.setPlatform("全部");
			}
			if(null==searchRequest.getProductId()){
				response.setProductId("全部");
			}
			if(null==searchRequest.getChannelId()){
				response.setChannelId("全部");
			}
			if(null==searchRequest.getActivityType()){
				response.setActivityType("全部");
			}
			if(StringUtils.isBlank(searchRequest.getActivityId())){
				response.setNewActivityId("全部");;
			}
			if(null==searchRequest.getEnvironment()){
				response.setEnvironment("全部");
			}
			if(StringUtils.isBlank(searchRequest.getPage())){
				response.setPage("全部");
			}
			if(StringUtils.isBlank(searchRequest.getCkModule())){
				response.setCkModule("全部");
			}
			if(StringUtils.isBlank(searchRequest.getPosition())){
				response.setPosition("全部");
			}
			if(null==searchRequest.getSourceId()){
				response.setSourceId("全部");
			}
			if(null==searchRequest.getLoginType()){
				response.setLoginType("全部");
			}
			if(StringUtils.isBlank(searchRequest.getUserType())){
				response.setUserType("全部");
			}else if(oldUser.equals(searchRequest.getUserType())){
				response.setUserType("老用户");
			}else if(newUser.equals(searchRequest.getUserType())){
				response.setUserType("新用户");
			}	
		}
		
		return list;
	}

}
